package 尊享面试.滑动窗口;

public class a1100长度为K的无重复字符子串 {
    static class Solution {
        public int numKLenSubstrNoRepeats(String s, int k) {
            int len = s.length();
            if (k > len) {
                return 0;
            }
            int ans = 0;
            int i = 0;
            int j = 0;
            int[] cnt = new int[26];
            while (i <= j && j < len) {
                while (cnt[s.charAt(j) - 'a'] > 0) {
                    cnt[s.charAt(i) - 'a']--;
                    i++;
                }
                cnt[s.charAt(j) - 'a']++;
                if (j - i + 1 == k) {
                    ans++;
                    cnt[s.charAt(i) - 'a']--;
                    i++;
                }
                j++;
            }

            return ans;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String S = "havefunonleetcode";
        int K = 5;
        System.out.println(solution.numKLenSubstrNoRepeats(S, K));
    }
}
